package com.shenfa.service.xml;

import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.xml.BaseParam;
import com.shenfa.bean.xml.Card;
import com.shenfa.bean.xml.UserBase;
import com.shenfa.bean.xml.Yyz;
import com.shenfa.dao.xml.MakeXmlDao;

public class MakeXmlService {
	@Autowired
	private MakeXmlDao makeXmlDao;
	/**
	 * 系统配置
	 */
	private Map<String,String> cfgMap;
	/**
	 * 待处理产品列表
	 */
	private List<Card> cardList;
	
	/**
	 * 待处理产品列表
	 */
	private List<UserBase> userList;
	
	private PXml px;
	private EXml ex;
	private Map<String,String> plog;
	private Map<String,String> elog;
	private String cyyz;
	private String cwbs;
	@Autowired
	private UserXml userXml;
	
	private boolean isPPrint;
	
	private StringBuffer xmlbase;
	private StringBuffer xmlp;
	private StringBuffer xmle;
	/**
	 * 预印纸列表
	 */
	private List<Yyz> yyzlist;
	
	private static Logger log=Logger.getLogger(MakeXmlService.class);
	
	public void make(String userid){
		int pcount=0;
		int ecount=0;
		//文件写入对象
		px=new PXml();
		ex=new EXml();
		String filepath="";
		//系统配置
		cfgMap=makeXmlDao.getConfig();
		//------------------
		BaseParam.PERIOD=cfgMap.get("PERIOD");
		//获取需要处理的产品
		cardList=makeXmlDao.getCardList(userid);
		for(Card card:cardList){
			filepath=cfgMap.get("BASEPATH")+card.getType()+"/SPEC/XML/";
			//创建电子账单
			this.createExml(card.getId(),filepath,userid);
			//设置产品类型
			cfgMap.put("cardid",card.getId());
			//查询卡对应的城市和外包商
			yyzlist=makeXmlDao.getYyzListByCardId(cfgMap);
			for(Yyz yyz:yyzlist){
				if(null==cyyz && null==cwbs){
					cwbs=yyz.getBusid();
					cyyz=yyz.getPaperNo();
					this.createPxml(card.getId(),yyz.getBusid(),yyz.getPaperNo(),filepath,userid);
				}else if(!cyyz.equals(yyz.getPaperNo()) || !cwbs.equals(yyz.getBusid())){
					cwbs=yyz.getBusid();
					cyyz=yyz.getPaperNo();
					this.colsePxml(pcount);//关闭文件写入
					pcount=0;
					this.createPxml(card.getId(),yyz.getBusid(),yyz.getPaperNo(),filepath,userid);
				}
				cfgMap.put("city",yyz.getCity());//保存城市id
				cfgMap.put("card",card.getId());//保存产品ID
				cfgMap.put("bus",yyz.getBusid());//保存预印纸ID
				cfgMap.put("userid", userid);
				userList=makeXmlDao.getUserBaseList(cfgMap);
				if(userList.size()>0){
					this.createPxml(card.getId(),yyz.getBusid(),yyz.getPaperNo(),filepath,userid);
				}
				for(UserBase ub:userList){
					isPPrint="Y".equals(ub.getPaperflag());
					xmlbase=new StringBuffer();
					//是否打印资质账单
					xmlbase.append("<checksheet>\n");
					//写入基本信息
					userXml.writeBaseXml(ub,xmlbase);
					//写入插槽信息
					userXml.writeEnvrule(cfgMap,xmlbase);
					//写入汇总信息
					if(userXml.writeAccinfoXml(ub,xmlbase)){
						log.error("没有汇总信息!账号="+ub.getAcctnbr());
						continue;
					}
					//写入汇总明细
					userXml.writeAccinfoDetail(ub,xmlbase);
					//账单购汇信息
					userXml.writeBuy(ub,xmlbase);
					//借记卡交易明细
					userXml.writeDebitinfo(ub,xmlbase);
					//积分数据
					userXml.writePoint(ub,xmlbase);
					//纸质模板个性化
					if(isPPrint){
						xmlp=new StringBuffer(xmlbase);
						
						xmlp.append(userXml.writeTemplate(ub,"1").toString());
						xmlp.append("</checksheet>\n");
						px.write(xmlp.toString());
						pcount++;
					}
					//电子模板个性化
					xmle=new StringBuffer(xmlbase);
					xmle.append(userXml.writeTemplate(ub,"2").toString());
					//一个电子账户写入结束
					xmle.append("</checksheet>\n");
					ex.write(xmle.toString());
					ecount++;
				}
			}
			this.colsePxml(pcount);//关闭文件写入
			this.colseExml(ecount);//关闭文件写入
		}
	}
	/**
	 * 创建电子账单
	 * @param cardid
	 * @param filepath
	 */
	public void createExml(String cardid,String filepath,String userid){
		String filename=ex.create(cardid,filepath);
		elog=new HashMap<String,String>();
		elog.put("filename",filename);
		elog.put("stmtdate",cfgMap.get("PERIOD"));//当前账期
		elog.put("wbs","");//外包商ID
		elog.put("card",cardid);//产品ID
		elog.put("yyz","");//预印纸ID
		elog.put("userid",userid);
		elog.put("btime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));//当前时间
		makeXmlDao.saveLog(elog);
	}
	/**
	 * 创建纸质账单
	 * @param card
	 * @param wbs
	 * @param yyz
	 * @param filepath
	 */
	public void createPxml(String card,String wbs,String yyz,String filepath,String userid){
		String filename=px.create(card,wbs,yyz,filepath);
		plog=new HashMap<String,String>();
		plog.put("filename",filename);
		plog.put("stmtdate",cfgMap.get("PERIOD"));//当前账期S_PERSON_NO
		plog.put("wbs",wbs);//外包商ID
		plog.put("card",card);//产品ID
		plog.put("yyz",yyz);//预印纸ID
		plog.put("userid",userid);
		plog.put("btime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));//当前时间
		makeXmlDao.saveLog(plog);
	}
	
	public void colsePxml(int count){
		px.over(count);
		plog.put("share",count+"");
		plog.put("etime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		plog.put("state","1");
		makeXmlDao.upLog(plog);
	}
	public void colseExml(int count){
		ex.over(count);
		elog.put("share",count+"");
		elog.put("etime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		elog.put("state","1");
		makeXmlDao.upLog(elog);
	}
}
